#include<bits/stdc++.h>
#define int long long
#define fir first
#define sec second
#define ls (u<<1)
#define rs (u<<1|1)
#define mid ((l+r)>>1)
#define Pr pair<int,int>
#define MP(i,j) make_pair(i,j)
#define Fo(i,j) for(auto i:j)
#define FO(i,j,k) for(int i=j;i;i=k)
#define PB(i) push_back(i)
#define FFF(i,j,k) for(auto i=j;i!=k;++i)
#define For(i,j,k) for(long long i=j;i<=k;++i)
#define FoR(i,j,k) for(long long i=j;i<k;++i)
#define FOR(i,j,k) for(long long i=j;i>=k;--i)
using namespace std;
template <typename _Tp>void read(_Tp& first) {
    _Tp x = 0, f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar();
    first = x * f;
}
#define Mod 147744151
#define N 1005
int n,m,Q,P,ans;
int a[N][N],b[N][N];
int ksm(int a,int b){
    int res=1;
    while(b){
        if(b&1)res=(res*a)%P;
        a=a*a%P;
        b>>=1;
    }return res;
}
signed main(){
	freopen("tsuki.in","r",stdin);
	freopen("tsuki.out","w",stdout);
    read(n),read(m),read(P);
    For(i,1,n)
    For(j,1,m)
    read(a[i][j]);
    read(Q);
    if(n<=200&&m<=200){
        For(k,1,Q){
            int sum=1;
            int x,y,s,t;
            read(s),read(t);
            read(x),read(y);
            For(i,s,x)
            For(j,t,y)
            sum=(sum*a[i][j])%P;
            ans=(ans+(k^sum)%Mod)%Mod;
            cout<<sum<<endl;
        }
        printf("%lld",ans);
        return 0;
    }
    For(i,1,n)
    For(j,1,m)
    b[i][j]=a[i][j];
    For(i,0,n)b[i][0]=1;
    For(i,0,m)b[0][i]=1;
    For(i,1,n)
    For(j,1,m)
//        if(i!=1&&j!=1)
		b[i][j]=b[i-1][j]*b[i][j-1]%P*ksm(b[i-1][j-1],P-2)%P*b[i][j]%P;
    /*For(i,1,n){
    	For(j,1,m)cout<<b[i][j]<<" ";
    	cout<<endl;
	}*/
    For(k,1,Q){
        int s,t,x,y,sum;
        read(s),read(t);
        read(x),read(y);
        sum=b[x][y]*b[s-1][t-1]%P*ksm(b[s-1][y],P-2)%P*ksm(b[x][t-1],P-2)%P;
//        cout<<sum<<endl;
        ans=(ans+(k^sum)%Mod)%Mod;
    }printf("%lld",ans);
	return 0;
}
/*
5 5 17
1 11 5 11 1
3 9 11 2 2
13 1 9 7 13
5 3 5 3 11
5 9 9 13 2
5
1 3 5 4
5 4 5 4
5 1 5 5
1 2 3 4
3 1 3 4
*/
